package mc.contents.shard;

import mc.support.orm.shard.ShardStrategy;

import java.util.Map;

/**
 * Created by LuoHui on 14-11-20.
 */
public class McKeywordStrategy implements ShardStrategy {
    private static final String TABLE_PRE = "mc_keyword.";
    private static final String SELLER_ID = "sellerId";
    private static final int COUNT = 40;

    @Override
    public String shardTableName( String baseTableName, Object params, String mapperId ) {
        Map paramMap = (Map) params;
        Long sellerId = (Long) paramMap.get( SELLER_ID );
        int mod = (int) ( sellerId % COUNT );
        return getTableName( baseTableName, mod );
    }

    private String getTableName( String baseTableName, int mod ) {
        return new StringBuilder().append( TABLE_PRE).append(baseTableName).append( '_' )
                .append( mod ).toString();
    }
}
